package com.example.creditapproval.mapper;

import com.example.creditapproval.model.Users;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

@Mapper
public interface UserMapper {

    @Insert("INSERT INTO users (name, password, user_role) VALUES (#{name}, #{password}, #{userRole})")
    @Options(useGeneratedKeys = true, keyProperty = "userId")
    int insertUser(Users users);

    @Select("SELECT * FROM users WHERE user_id = #{userId}")
    @Results({
            @Result(column = "user_id", property = "userId", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
            @Result(column = "password", property = "password", jdbcType = JdbcType.VARCHAR),
            @Result(column = "user_role", property = "userRole", jdbcType = JdbcType.INTEGER)
    })
    Users getUserById(Integer userId);

    @Update("UPDATE users SET name=#{name}, password=#{password}, user_role=#{userRole} WHERE user_id=#{userId}")
    int updateUser(Users users);

    @Delete("DELETE FROM users WHERE user_id=#{userId}")
    int deleteUser(Integer userId);
}
